package com.ciwong.mobilelib.widget.clipimage;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.Region;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

/**
 * 剪切头像边界
 */
public class ClipImageBorderView extends View
{
    /**
     * 水平方向与View的边距
     */
    private int mHorizontalPadding;

    /**
     * 垂直方向与View的边距
     */
    private int mVerticalPadding;

    /**
     * 绘制的矩形宽度
     */
    private int clipFrameWidth;

    /**
     * 绘制的矩形高度
     */
    private int clipFrameHeight;

    /**
     * 边框的颜色，默认为白色
     */
    private int mBorderColor = Color.parseColor("#9ad3f4");

    /**
     * 边框的宽度 单位dp
     */
    private int mBorderWidth = 2;

    private Paint mPaint;

    private Rect drawRect;

    private Rect rectLeftTop, rectLeftBottom, rectRightTop, rectRightBottom;

    private Rect currentTouchRect;

    private int ratio = 15;

    /**
     * 是否希望可改变改变框
     */
    private boolean needScale = true;

    /**
     * 是否可以改变框大小
     */
    private boolean isCanScale = false;

    private int moveY, moveX;

    private IClipBorderChange mIClipBorderChange;

    public ClipImageBorderView(Context context)
    {
        this(context, null);
    }

    public ClipImageBorderView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public ClipImageBorderView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);

        mBorderWidth = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, mBorderWidth, getResources()
                        .getDisplayMetrics());
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        rectLeftTop = new Rect();
        rectLeftBottom = new Rect();
        rectRightTop = new Rect();
        rectRightBottom = new Rect();
        ratio = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                ratio, getResources().getDisplayMetrics());
        setClickable(true);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        // 计算距离屏幕垂直橫向边界 的边距
        mVerticalPadding = (getHeight() - clipFrameHeight) / 2;
        mHorizontalPadding = (getWidth() - clipFrameWidth) / 2;

        mPaint.setColor(Color.parseColor("#88000000"));
        mPaint.setStyle(Style.FILL);

        int left = mHorizontalPadding;
        int top = mVerticalPadding;
        int right = getWidth() - mHorizontalPadding;
        int bottom = getHeight() - mVerticalPadding;

        canvas.save();
        canvas.clipRect(left, top, right, bottom, Region.Op.DIFFERENCE);

        // // 绘制左边1
        canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);
        // // 绘制右边2
        // canvas.drawRect(getWidth() - mHorizontalPadding, 0, getWidth(),
        // getHeight(), mPaint);
        // // 绘制上边3
        // canvas.drawRect(mHorizontalPadding, 0, getWidth() -
        // mHorizontalPadding,
        // mVerticalPadding, mPaint);
        // // 绘制下边4
        // canvas.drawRect(mHorizontalPadding, getHeight() - mVerticalPadding,
        // getWidth() - mHorizontalPadding, getHeight(), mPaint);
        // 绘制外边框
        mPaint.setColor(mBorderColor);
        mPaint.setStrokeWidth(mBorderWidth);
        mPaint.setStyle(Style.STROKE);

        drawRect = new Rect(left, top, right, bottom);
        // mPaint.setColor(Color.TRANSPARENT);
        canvas.restore();
        canvas.drawRect(drawRect, mPaint);
        if (needScale)
        {
            mPaint.setStyle(Style.FILL);
            mPaint.setStrokeWidth(mBorderWidth * 2);
            // mPaint.setXfermode(new
            // PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            setRect(canvas, rectLeftTop, left, top);
            setRect(canvas, rectRightTop, right, top);
            setRect(canvas, rectRightBottom, right, bottom);
            setRect(canvas, rectLeftBottom, left, bottom);
        }
        // mPaint.setXfermode(null);
    }

    private void setRect(Canvas canvas, Rect r, int x, int y)
    {
        if (r == null)
        {
            return;
        }

        r.set(x - ratio, y - ratio, x + ratio, y + ratio);
        canvas.drawCircle(x, y, ratio / 2, mPaint);
    }

    public void setNeedScale(boolean needScale)
    {
        this.needScale = needScale;
    }

    private boolean checkContains(int x, int y)
    {
        currentTouchRect = null;

        if (rectLeftTop.contains(x, y))
        {
            currentTouchRect = rectLeftTop;
        }
        if (rectLeftBottom.contains(x, y))
        {
            currentTouchRect = rectLeftBottom;
        }
        if (rectRightBottom.contains(x, y))
        {
            currentTouchRect = rectRightBottom;
        }
        if (rectRightTop.contains(x, y))
        {
            currentTouchRect = rectRightTop;
        }
        return currentTouchRect != null;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        int action = event.getAction();
        int x = (int) event.getX();
        int y = (int) event.getY();
        if (action == MotionEvent.ACTION_DOWN)
        {
            if (needScale)
            {
                if (checkContains(x, y))
                {
                    isCanScale = true;
                    moveX = x;
                    moveY = y;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }

        }
        else if (action == MotionEvent.ACTION_MOVE)
        {
            if (isCanScale && needScale)
            {
                int delX = x - moveX;
                int delY = y - moveY;

                changeClipFrameSize(delX, delY);
                invalidate();
                moveX = x;
                moveY = y;
            }
        }
        else if (action == MotionEvent.ACTION_UP
                || action == MotionEvent.ACTION_CANCEL)
        {
            isCanScale = false;
            moveX = moveY = 0;
        }
        return super.onTouchEvent(event);
    }

    private void changeClipFrameSize(int delX, int delY)
    {
        if (currentTouchRect == rectLeftTop)
        {
            delX = -delX;
            delY = -delY;
        }
        else if (currentTouchRect == rectLeftBottom)
        {
            delX = -delX;
        }
        else if (currentTouchRect == rectRightTop)
        {
            delY = -delY;
        }

        initClipFrameSize(clipFrameWidth + delX, clipFrameHeight + delY);
    }

    /**
     * 剪切框大小
     * 
     * @param clip_width
     *            剪切的宽
     * @param clip_height
     *            剪切的高
     */
    public void initClipFrameSize(int clip_width, int clip_height)
    {
        if (clip_width >= 2 * ratio)
        {
            clipFrameWidth = clip_width;
        }
        if (clip_height >= 2 * ratio)
        {
            clipFrameHeight = clip_height;
        }
        if (mIClipBorderChange != null)
        {
            mIClipBorderChange.change(mHorizontalPadding, mVerticalPadding,
                    clip_width, clip_height);
        }
    }

    public void setIClipBorderChange(IClipBorderChange i)
    {
        mIClipBorderChange = i;
    }

    public interface IClipBorderChange
    {
        void change(int horizontalPadding, int verticalPadding, int width,
                int height);
    }
}
